package com.huawei.crm.sample.adapter.inbound.rest.product;

import com.huawei.crm.sample.adapter.inbound.rest.product.converter.ProductReqConverter;
import com.huawei.crm.sample.adapter.inbound.rest.product.converter.ProductVOConverter;
import com.huawei.crm.sample.adapter.inbound.rest.product.request.ProductReq;
import com.huawei.crm.sample.adapter.inbound.rest.product.vo.ProductVO;
import com.huawei.crm.sample.application.product.ProductUseCase;
import com.huawei.crm.sample.application.product.dto.ProductDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    private final ProductUseCase productUseCase;
    private final ProductReqConverter productReqConverter;
    private final ProductVOConverter productVOConverter;

    @Autowired
    public ProductController(ProductUseCase productUseCase, ProductReqConverter productReqConverter, ProductVOConverter productVOConverter) {
        this.productUseCase = productUseCase;
        this.productReqConverter = productReqConverter;
        this.productVOConverter = productVOConverter;
    }

    @PostMapping
    public ProductVO createProduct(@RequestBody ProductReq productReq) {
        ProductDTO productDTO = productReqConverter.toDTO(productReq);
        ProductDTO result = productUseCase.createProduct(productDTO);
        return productVOConverter.toVO(result);
    }

    @GetMapping("/{productId}")
    public ProductVO getProduct(@PathVariable String productId) {
        ProductDTO result = productUseCase.getProduct(productId);
        return productVOConverter.toVO(result);
    }

    @PostMapping("/{productId}/decreaseStock")
    public void decreaseStock(@PathVariable String productId, @RequestParam int quantity) {
        productUseCase.decreaseStock(productId, quantity);
    }
} 